﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using ShredTheLog.Api.DataContracts;
using ShredTheLog.Api;
using System.ComponentModel.Composition;
using System.Diagnostics.Contracts;

namespace ShredTheLog.Converters
{
    [ValueConversion(typeof(bool), typeof(string))]
    public sealed class YesNoConverter : ConverterBase, IValueConverter
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Может прийти null (например, при подвязке к полям типа bool?).
            if (value != null)
            {
                bool v = (bool)value;
                Type enumType = typeof(YesNo);
                string key = String.Format("s_{0}_{1}", enumType.Name, Enum.GetName(enumType, v ? YesNo.Yes : YesNo.No));
                Contract.Assume(!String.IsNullOrWhiteSpace(key));
                return ShredServices.GetLocalizedString(key);
            }
            else
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        public static readonly YesNoConverter Default = new YesNoConverter();
    }
}
